<?php
namespace App\Http\Controllers;

/**
 * 
 * 短信控制器
 * 
 * @author maqu
 *
 */
use App\Services\SMSService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;

class SMSController extends BaseController {

    //获取验证码
    public function sendcode(){

        $phone = Input::get('phone');
        $codetype = Input::get('codetype');

        if(!$phone || !$codetype){
            return $this->args_invalid();
        }

        if($codetype !=SMS_CODE_TYPE_REGISTER){
            $count=DB::table('member')
                ->where('user_state',0)
                ->where('user_type',USER_TYPE_CONSUMER)
                ->where('user_name',$phone)->count();

            if($count==0){
                return $this->failure('账户不存在');
            }
        }

        $smsService = new SMSService();
        $result = $smsService->sendCode($phone,$codetype);
        if($result['result']){
            return $this->success();
        } else {
            return $this->failure($result['msg']);
        }

    }
    
    //验证验证码
    public function checkcode(){
    
        $phone = Input::get('phone');
        $code = Input::get('code');
        $codetype = Input::get('codetype');

        if(!$phone || !$codetype|| !$code){
            return $this->args_invalid();
        }

        $smsService = new SMSService();
        $result = $smsService->checkCode($phone,$codetype,$code);
        if($result['result']){
            return $this->success();
        } else {
            return $this->failure($result['msg']);
        }

    }

}